"""add_grades_and_knowledge_points

Revision ID: f8723257585a
Revises: 12dac5aaa91d
Create Date: 2025-09-17 15:40:53.091820

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision: str = 'f8723257585a'
down_revision: Union[str, None] = '12dac5aaa91d'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('grades',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('name', sa.String(length=50), nullable=False),
    sa.Column('level', sa.Integer(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('level'),
    sa.UniqueConstraint('name')
    )
    with op.batch_alter_table('grades', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_grades_id'), ['id'], unique=False)

    op.create_table('knowledge_points',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('name', sa.String(length=100), nullable=False),
    sa.Column('description', sa.String(length=500), nullable=True),
    sa.Column('grade_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['grade_id'], ['grades.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('knowledge_points', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_knowledge_points_id'), ['id'], unique=False)

    with op.batch_alter_table('questions', schema=None) as batch_op:
        batch_op.add_column(sa.Column('grade_id', sa.Integer(), nullable=False))
        batch_op.add_column(sa.Column('knowledge_point_id', sa.Integer(), nullable=False))
        batch_op.create_foreign_key(None, 'knowledge_points', ['knowledge_point_id'], ['id'])
        batch_op.create_foreign_key(None, 'grades', ['grade_id'], ['id'])
        batch_op.drop_column('grade')
        batch_op.drop_column('knowledge_points')

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('questions', schema=None) as batch_op:
        batch_op.add_column(sa.Column('knowledge_points', mysql.JSON(), nullable=False))
        batch_op.add_column(sa.Column('grade', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False))
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_column('knowledge_point_id')
        batch_op.drop_column('grade_id')

    with op.batch_alter_table('knowledge_points', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_knowledge_points_id'))

    op.drop_table('knowledge_points')
    with op.batch_alter_table('grades', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_grades_id'))

    op.drop_table('grades')
    # ### end Alembic commands ###
